package com.situ.feemanagement.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.feemanagement.listener.MedicineExcelListener;
import com.situ.feemanagement.mapper.MedicineMapper;
import com.situ.feemanagement.pojo.entity.Medicine;
import com.situ.feemanagement.pojo.query.MedicineQuery;
import com.situ.feemanagement.pojo.vo.QuantityVO;
import com.situ.feemanagement.pojo.vo.MedicineExcelVO;
import com.situ.feemanagement.pojo.vo.MedicineVO;
import com.situ.feemanagement.service.IMedicineService;
import com.situ.feemanagement.util.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MedicineServiceImpl implements IMedicineService {

    @Autowired
    private MedicineMapper medicineMapper;

    @Override
    public List<Medicine> selectAll() {
        return medicineMapper.selectAll();
    }

    @Override
    public Map<String, Object> selectByPage(MedicineQuery medicineQuery) {
        PageHelper.startPage(medicineQuery.getPage(), medicineQuery.getLimit());
        List<MedicineVO> list = medicineMapper.selectByPage(medicineQuery);
        PageInfo<MedicineVO> pageInfo = new PageInfo<>(list);
        int totalCount = (int) pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", totalCount);
        map.put("list", list);
        return map;
    }

    @Override
    public void deleteById(int id) {
        medicineMapper.deleteById(id);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            medicineMapper.deleteById(id);
        }
    }

    @Override
    public void add(Medicine medicine) {
        medicineMapper.add(medicine);
    }

    @Override
    public Medicine selectById(int id) {
        return medicineMapper.selectById(id);
    }

    @Override
    public void update(Medicine medicine) {
        medicineMapper.update(medicine);
    }

    @Override
    public List<QuantityVO> selectQuantity(int id) {
        List<QuantityVO> list = new ArrayList<>();
        Medicine medicine = medicineMapper.selectById(id);
        for (int i = 1; i <= medicine.getInventory(); i++) {
            QuantityVO quantityVO = new QuantityVO();
            quantityVO.setQuantity(i);
            list.add(quantityVO);
        }
        return list;
    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        List<Medicine> list = medicineMapper.selectAll();
        List<MedicineExcelVO> medicineExcelVOList = new ArrayList<>();
        for (Medicine medicine : list) {
            MedicineExcelVO medicineExcelVO = new MedicineExcelVO();
            BeanUtils.copyProperties(medicine, medicineExcelVO);
            medicineExcelVOList.add(medicineExcelVO);
        }
        ExcelUtil.exportExcel(response, medicineExcelVOList, MedicineExcelVO.class, "medicine");
    }

    @Override
    public void importExcel(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), MedicineExcelVO.class, new MedicineExcelListener(medicineMapper)).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
